/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.esm.pdafx.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.esm.pdafx.R;
import com.esm.pdafx.database.dbmodel.SystemAllInfo;
import com.esm.pdafx.database.manager.SystemCenter;
import com.esm.pdafx.model.WBox;
import com.esm.pdafx.util.HttpRequest;
import com.google.zxing.WriterException;
import com.zxing.encoding.EncodingHandler;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.manager.CacheManager;
import zuo.biao.library.util.SettingUtil;

public class AboutActivity extends BaseActivity implements OnClickListener, OnLongClickListener, OnBottomDragListener {
    private static final String TAG = "AboutActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, AboutActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_activity, this);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        //仅测试用
        HttpRequest.translate("library", 0, new OnHttpResponseListener() {

            @Override
            public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                showShortToast("测试Http请求:翻译library结果为\n" + resultJson);
            }
        });

        test();
    }

    void test() {
        //存取数据库
        SystemCenter.getInstance().saveSysInfo(new SystemAllInfo(null, "username", "colin1"));
        SystemAllInfo user = SystemCenter.getInstance().getSysInfo("username");
        if (user != null) {
            String username = user.getContent();
            showShortToast("username = " + username);
        }

        //存取SharedPreferences Key-value
        SharedPreferences sp = getSharedPreferences("colininfo", Context.MODE_PRIVATE);
        boolean frommainstart = sp.getBoolean("frommainstart", false);
        Log.d("MainService", "Service1-onCreate-- ");
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("frommainstart", true);
        editor.commit();

        //CacheManager
        //单个对象保存---还是用SharedPreferences
        long wboxId = 11;
        WBox wbox = new WBox(wboxId, "1231231");
        CacheManager.getInstance().save(WBox.class, wbox, "" + wbox.getId());
        wbox = CacheManager.getInstance().get(WBox.class, "" + wboxId);

        //list对象保存
        runThread(TAG + "loadData", new Runnable() {
            @Override
            public void run() {
                //耗时的IO操作使用线程； 线程内不能刷UI或toast
                ArrayList<WBox> wboxList = new ArrayList<WBox>();
                long time = System.currentTimeMillis();
                for (int i = 0; i < 10; i++) {
                    wboxList.add(new WBox(time + i, time + "-" + (i + 1)));
                }

                LinkedHashMap<String, WBox> map = new LinkedHashMap<String, WBox>();
                for (WBox data : wboxList) {
                    if (data != null) {
                        map.put(data.getId() + "", data);//map.put(null, data);不会崩溃
                    }
                }
                CacheManager.getInstance().addList(WBox.class, map);

                List<WBox> wboxList2 = CacheManager.getInstance().getList(WBox.class, 0, 5);

                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //耗时线程结束后刷UI

                    }
                });
            }
        });
        //
        wboxId = 2;

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    private ImageView ivAboutQRCode;

    @Override
    public void initView() {
        ivAboutQRCode = findView(R.id.ivAboutQRCode, this);

        TextView tvIconshow = (TextView)findViewById(R.id.tvIconshow);
        tvIconshow.setTypeface(AppContext.getInstances().getIconfont());
        tvIconshow.setText(getString(R.string.fonticon_import));
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initData() {

        setQRCode();
    }


    private Bitmap qRCodeBitmap;

    /**
     * 显示二维码
     */
    protected void setQRCode() {
        runThread(TAG + "setQRCode", new Runnable() {

            @Override
            public void run() {

                try {
                    qRCodeBitmap = EncodingHandler.createQRCode("http://106.15.192.154:8883/dahui_login/"
                            , (int) (2 * getResources().getDimension(R.dimen.qrcode_size)));
                } catch (WriterException e) {
                    e.printStackTrace();
                    Log.e(TAG, "initData  try {Bitmap qrcode = EncodingHandler.createQRCode(contactJson, ivContactQRCodeCode.getWidth());" +
                            " >> } catch (WriterException e) {" + e.getMessage());
                }

                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ivAboutQRCode.setImageBitmap(qRCodeBitmap);
                    }
                });
            }
        });
    }


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initEvent() {

    }


    @Override
    public void onDragBottom(boolean rightToLeft) {
        if (rightToLeft) {
            return;
        }

        if (SettingUtil.isFirstStart) {
            runThread(TAG + "onDragBottom", new Runnable() {
                @Override
                public void run() {
                    Log.i(TAG, "onDragBottom  >> SettingUtil.putBoolean(context, SettingUtil.KEY_IS_FIRST_IN, false);");
                    SettingUtil.putBoolean(SettingUtil.KEY_IS_FIRST_START, false);
                }
            });
        }

        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivAboutQRCode:
                showShortToast("ivAboutQRCode");
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
        return false;
    }


    //生命周期、onActivityResult<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //生命周期、onActivityResult>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}
